package ntu.p2p.dht.test;

import java.io.IOException;

import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.Id;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import ntu.p2p.dht.DHTContentKey;
import ntu.p2p.past.messaging.UpdateObjectMessage;
import ntu.p2p.past.test.Diff;

public class Key implements DHTContentKey {

	private Id id;
	private int offset;
	
	public static Key build(final InputBuffer buf, Endpoint endpoint) throws IOException {
		return new Key(buf, endpoint);
  }  
  
	protected Key(final InputBuffer buf, Endpoint endpoint) throws IOException {
		this.offset = buf.readInt();
		id = endpoint.readId(buf, buf.readShort());
		System.out.println("offset: " + this.offset);
	}
	
	public Key(Id id, int offset) {
		this.id = id;
		this.offset = offset;
	}
	
	@Override
	public Id getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		System.out.println("original : " + this.offset);
		buf.writeInt(this.offset);
		buf.writeShort(this.id.getType());
		this.id.serialize(buf);

	}
}
